package com.sky.config;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.TopReport;
import com.sky.mapper.*;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.sky.constant.RedisConstant.DISH_KRY;
import static com.sky.constant.RedisConstant.SETMEAL_KRY;

/**
 * 缓存预热
 */
@Component
@RequiredArgsConstructor
public class RedisPreheatRunner implements CommandLineRunner {
    private final StringRedisTemplate redisTemplate;
    private final SetmealMapper setmealMapper;
    private final DishFlavorMapper dishflavorMapper;
    private final DishMapper dishMapper;
    private final ReportMapper reportMapper;

    @Override
    public void run(String... args) throws Exception {
        //清楚缓存
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
        //获取所有套餐信息进行套餐预热
        List<Setmeal> list = setmealMapper.listAllSetmeal();
        if (CollUtil.isNotEmpty(list)) {
            Map<Long, List<Setmeal>> listMap = list.stream().collect(Collectors.groupingBy(Setmeal::getCategoryId));
            ListOperations<String, String> redisList = redisTemplate.opsForList();
            listMap.forEach((k, v) -> {
                ArrayList<String> setmealList = new ArrayList<>();
                for (Setmeal setmeal : v) {
                    setmealList.add(JSON.toJSONString(setmeal));
                }
                //将套餐信息存储到Redis中
                redisList.leftPushAll(SETMEAL_KRY + k, setmealList);
            });
        }
        //获取所有菜品信息
        List<Dish> dishList = dishMapper.listAllDish();
        ListOperations<String, String> redisList = redisTemplate.opsForList();
        ArrayList<DishVO> dishVOS = new ArrayList<>();
        dishList.forEach(dish -> {
            DishVO dishVO = BeanUtil.copyProperties(dish, DishVO.class);
            //获取菜品口味
            List<DishFlavor> dishFlavors = dishflavorMapper.listByDishId(dishVO.getId());
            dishVO.setFlavors(dishFlavors);
            dishVOS.add(dishVO);
        });
        dishVOS.forEach(item -> {
            redisList.leftPushAll(DISH_KRY + item.getCategoryId(), JSONObject.toJSONString(item));
        });
    }
}
